<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>菜鸟教程</title>
    <style>
      div {
        width: 800px;
        margin: 200px auto;
        color: red;
        text-align: center;
        font: 600 30px/30px "simsun";
      }
    </style>
  </head>
  <body>
    <div></div>

    <script type="text/javascript">
      var date1 = new Date();
      console.log(date1);
      console.log(typeof date1);

      console.log(formatDate());

      function formatDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();

        var week = date.getDay();
        var weekArray = [
          "星期日",
          "星期一",
          "星期二",
          "星期三",
          "星期四",
          "星期五",
          "星期六",
        ];

        var hour = date.getHours();
        hour = hour < 10 ? "0" + hour : hour;

        var minute = date.getMinutes();
        minute = minute < 10 ? "0" + minute : minute;

        var second = date.getSeconds();
        second = second < 10 ? "0" + second : second;

        var result =
          "今天是：" +
          year +
          "年" +
          month +
          "月" +
          day +
          "日 " +
          hour +
          ":" +
          minute +
          ":" +
          second +
          " " +
          weekArray[week];
        return result;
      }

      const div = document.getElementsByTagName("div")[0];
      div.innerText = formatDate();
    </script>
  </body>
</html>
